//
//  BVIndexViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/9/28.
//  Copyright © 2016年 com.yinding. All rights reserved.
//  济南市id 370100

#import "BVIndexViewController.h"
#import "BVIndexTool.h"
#import "BVBannerCell.h"
#import "BVButtonCell.h"
#import "BVTimeLimitCell.h"
#import "UIView+Extension.h"
#import "BVHeaderReusableView.h"
#import "BVSearchBar.h"
#import "BVGoodsDetailViewController.h"
#import "BVSearchViewController.h"
#import "BVIndexTool.h"
#import "MBProgressHUD.h"
#import "BVChangeCityViewController.h"
#import "BVNavItem.h"

#import "BVCategoryViewController.h"
#import "BVMyOrderViewController.h"
#import "BVRechargeViewController.h"
#import "BVNavigationController.h"

#define BANNER_Cell_IDENTIFIER @"BVBannerCell"
#define BUTTON_CELL_IDENTIFIER @"BVButtonCell"
#define TIMELIMIT_CELL_IDENTIFIER @"BVTimeLimitCell"
#define HEADER_REUSABLEVIEW_IDENTIFIER @"BVHeaderReusableView"

@interface BVIndexViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,BVIndexBannerCellDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSources;
@property (nonatomic, strong) BVSearchBar *searchBar;
@property (nonatomic, strong) BVIndexTool *indexTool;
@property (nonatomic, strong) UIButton *cityItem;

@end

@implementation BVIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarItem];
    [self fetchIndexData];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!self.user.city && self.user.city_id) {
        BVChangeCityViewController *cityVC = [[BVChangeCityViewController alloc] init];
        [self.navigationController pushViewController:cityVC animated:YES];
    }
    [self.cityItem setTitle:self.user.city forState:UIControlStateNormal];
    NSLog(@"%@",self.user.city_id);
}

- (void)fetchIndexData {

}


- (BVIndexTool *)indexTool {
    if (!_indexTool) {
        _indexTool = [[BVIndexTool alloc] init];
    }
    return _indexTool;
}

-(NSMutableArray *)dataSources {
    if (!_dataSources) {
        NSArray *array = [NSArray arrayWithObjects:[UIImage imageNamed:@"banner1.jpeg"],[UIImage imageNamed:@"banner2.png"],[UIImage imageNamed:@"banner3.jpg"],[UIImage imageNamed:@"banner4.jpg"], nil];
        _dataSources = (NSMutableArray *)array;
    }
    return _dataSources;
}

-(void)setCollectionView:(UICollectionView *)collectionView {
    _collectionView = collectionView;
    [collectionView registerClass:[BVBannerCell class] forCellWithReuseIdentifier:BANNER_Cell_IDENTIFIER];
    [collectionView registerNib:NIB_NAMED(BUTTON_CELL_IDENTIFIER) forCellWithReuseIdentifier:BUTTON_CELL_IDENTIFIER];
    [collectionView registerNib:NIB_NAMED(TIMELIMIT_CELL_IDENTIFIER) forCellWithReuseIdentifier:TIMELIMIT_CELL_IDENTIFIER];
    // 注册头视图
    [collectionView registerNib:NIB_NAMED(HEADER_REUSABLEVIEW_IDENTIFIER) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HEADER_REUSABLEVIEW_IDENTIFIER];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)setupNavBarItem {
    
    _searchBar = [[BVSearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW - 105, 30)];
    _searchBar.delegate = self;
    self.navigationItem.titleView = _searchBar;
    
    BVNavItem *cityItem = [[BVNavItem alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    [cityItem setTitle:self.user.city forState:UIControlStateNormal];
    [cityItem addTarget:self action:@selector(cityClick) forControlEvents:UIControlEventTouchUpInside];
    self.cityItem = cityItem;
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(cityItem.frame), 5, 14, 10)];
    [imageV setImage:IMAGE_NAMED(@"white_arrow.png")];
    
    UIView *navItem = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cityItem.width + imageV.width, 20)];
    [navItem addSubview:cityItem];
    [navItem addSubview:imageV];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -10;
    self.navigationItem.leftBarButtonItems = @[space,[[UIBarButtonItem alloc] initWithCustomView:navItem]];
}

#pragma mark -
#pragma mark - 选择地区
- (void)cityClick {
    BVNavigationController *nav = [[BVNavigationController alloc] initWithRootViewController:[[BVChangeCityViewController alloc] init]];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 
#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 1;
    }
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        BVBannerCell *bannerCell = [collectionView dequeueReusableCellWithReuseIdentifier:BANNER_Cell_IDENTIFIER forIndexPath:indexPath];
        bannerCell.delegate = self;
        bannerCell.dataSource = self.dataSources;
        return bannerCell;

    } else if (indexPath.section == 1) {
        BVButtonCell *buttonCell = [collectionView dequeueReusableCellWithReuseIdentifier:BUTTON_CELL_IDENTIFIER forIndexPath:indexPath];
        for (UIButton *btn in buttonCell.indexButtonArray) {
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return buttonCell;
    
    } else {
        BVTimeLimitCell *timeLimitCell = [collectionView dequeueReusableCellWithReuseIdentifier:TIMELIMIT_CELL_IDENTIFIER forIndexPath:indexPath];
        return timeLimitCell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(ScreenW, 180);
    }else if (indexPath.section == 1) {
        return CGSizeMake(ScreenW, 100);
    }
    return CGSizeMake(ScreenW / 3 , 190);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    BVHeaderReusableView *headerView = (BVHeaderReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HEADER_REUSABLEVIEW_IDENTIFIER forIndexPath:indexPath];
    
    [headerView.moreButton addTarget:self action:@selector(moreInfoClick) forControlEvents:UIControlEventTouchUpInside];
    return headerView;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return section == 2 ? CGSizeMake(ScreenW, 35):CGSizeZero;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return section == 2 ? UIEdgeInsetsMake(.5, 0, .5, 0):UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        BVGoodsDetailViewController *goodsDetail = [[BVGoodsDetailViewController alloc] init];
        
        [self.navigationController pushViewController:goodsDetail animated:YES];
    }
}

#pragma mark - 
#pragma mark - 四个按钮响应事件
- (void)buttonClick:(UIButton *)sender {

    switch (sender.tag) {
        case 1:{
            self.tabBarController.selectedIndex = 1;
        }break;
            
        case 2:{
            BVMyOrderViewController *order = [[BVMyOrderViewController alloc] init];
            [self.navigationController pushViewController:order animated:YES];
        }break;
            
        case 3:{
            
        }break;
        
        case 4:{
            BVRechargeViewController *recharge = [[BVRechargeViewController alloc] init];
            [self.navigationController pushViewController:recharge animated:YES];
        }break;
            
        default:break;
    }
}

#pragma mark -
#pragma mark -更多响应事件
- (void)moreInfoClick {
    
}

#pragma mark -
#pragma mark - BVIndexBannerCellDelegate
- (void)indexBannerCell:(BVBannerCell *)cell didSelectedIndex:(NSInteger)index {
}

#pragma mark - 
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self.searchBar resignFirstResponder];

    BVNavigationController *nav = [[BVNavigationController alloc] initWithRootViewController:[[BVSearchViewController alloc] init]];
    [self presentViewController:nav animated:NO completion:nil];
}

@end
